﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class index : System.Web.UI.Page
{
    string strCon = System.Configuration.ConfigurationManager.ConnectionStrings["xxnewsConnectionString"].ConnectionString;
    SqlConnection sqlcon;
    //SqlCommand sqlcom;
    string chejiansql = "SELECT [ch_pwname] FROM [siteman] WHERE ch_pwname in(SELECT DISTINCT chejian1 FROM tcjcb_qiche) or ch_pwname in(SELECT DISTINCT chejian1 FROM tcjcb) group by ch_pwname,ch_px ORDER BY [ch_px]";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            for (int i =Convert.ToInt32(DateTime.Now.Year) ; i >=2012 ; i--)
            {
                nian.Items.Add(i.ToString());
            }
            for (int i = 1; i <= 12; i++)
            {
                yue.Items.Add(i.ToString());
            }
            nian.SelectedValue = DateTime.Now.Year.ToString();
            //yue.SelectedValue = DateTime.Now.Month.ToString();
            SqlDataSource_chejian.SelectCommand = chejiansql;
            GridView1.Attributes.Add("bordercolor", "#bbddff");
            g_bind();
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#D1DDF1'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
           
            //当有编辑列时，避免出错，要加的RowState判断 
            //if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            //{
            //    ((LinkButton)e.Row.Cells[17].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除这条记录吗?')");
            //}
        }
        if (e.Row.RowIndex != -1)
        {
            int id = e.Row.RowIndex + 1 + GridView1.PageIndex * GridView1.PageSize;
            e.Row.Cells[0].Text = id.ToString();
        }
    }
    protected void DropDownList_lei_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList_lei.SelectedValue == "请选择")
        {
            DropDownList_sscj.Items.Clear();
            DropDownList_sscj.Items.Add(new ListItem("请选择"));
            SqlDataSource_chejian.SelectCommand = chejiansql;
            DropDownList_zrbz.Items.Clear();
            DropDownList_zrbz.Items.Add(new ListItem("请选择"));
            DropDownList_chehao.Items.Clear();
            DropDownList_chehao.Items.Add(new ListItem("请选择"));
        }
        else if (DropDownList_lei.SelectedValue == "汽车")
        {
            DropDownList_sscj.Items.Clear();
            DropDownList_sscj.Items.Add(new ListItem("请选择"));
            SqlDataSource_chejian.SelectCommand = "SELECT [ch_pwname] FROM [siteman] WHERE ch_pwname in(SELECT DISTINCT chejian1 FROM tcjcb_qiche) ORDER BY [ch_px]";
            DropDownList_zrbz.Items.Clear();
            DropDownList_zrbz.Items.Add(new ListItem("请选择"));
            DropDownList_chehao.Items.Clear();
            DropDownList_chehao.Items.Add(new ListItem("请选择"));
        }
        else if (DropDownList_lei.SelectedValue == "轨道车")
        {
            DropDownList_sscj.Items.Clear();
            DropDownList_sscj.Items.Add(new ListItem("请选择"));
            SqlDataSource_chejian.SelectCommand = "SELECT [ch_pwname] FROM [siteman] WHERE ch_pwname in(SELECT DISTINCT chejian1 FROM tcjcb) ORDER BY [ch_px]";
            DropDownList_zrbz.Items.Clear();
            DropDownList_zrbz.Items.Add(new ListItem("请选择"));
            DropDownList_chehao.Items.Clear();
            DropDownList_chehao.Items.Add(new ListItem("请选择"));
        }
        else if (DropDownList_lei.SelectedValue == "发电机")
        {
            DropDownList_sscj.Items.Clear();
            DropDownList_sscj.Items.Add(new ListItem("请选择"));
            SqlDataSource_chejian.SelectCommand = "SELECT [ch_pwname] FROM [siteman] WHERE ch_pwname in(SELECT DISTINCT chejian1 FROM tcjcb_fadianji) ORDER BY [ch_px]";
            DropDownList_zrbz.Items.Clear();
            DropDownList_zrbz.Items.Add(new ListItem("请选择"));
            DropDownList_chehao.Items.Clear();
            DropDownList_chehao.Items.Add(new ListItem("请选择"));
        }
    }
    protected void DropDownList_sscj_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList_lei.SelectedValue == "请选择")
        {
            DropDownList_zrbz.Items.Clear();
            DropDownList_zrbz.Items.Add(new ListItem("请选择"));
            SqlDataSource_banzu.SelectCommand = "";
            DropDownList_chehao.Items.Clear();
            DropDownList_chehao.Items.Add(new ListItem("请选择"));
        }
        else if (DropDownList_lei.SelectedValue == "汽车")
        {
            DropDownList_zrbz.Items.Clear();
            DropDownList_zrbz.Items.Add(new ListItem("请选择"));
            SqlDataSource_banzu.SelectCommand = "SELECT [ch_pwname] FROM [siteman] WHERE ch_pwname in(SELECT DISTINCT banzu1 FROM tcjcb_qiche WHERE chejian1='" + DropDownList_sscj.SelectedValue + "') ORDER BY [ch_px]";
            DropDownList_chehao.Items.Clear();
            DropDownList_chehao.Items.Add(new ListItem("请选择"));
        }
        else if (DropDownList_lei.SelectedValue == "轨道车")
        {
            DropDownList_zrbz.Items.Clear();
            DropDownList_zrbz.Items.Add(new ListItem("请选择"));
            SqlDataSource_banzu.SelectCommand = "SELECT [ch_pwname] FROM [siteman] WHERE ch_pwname in(SELECT DISTINCT banzu1 FROM tcjcb WHERE chejian1='" + DropDownList_sscj.SelectedValue + "') ORDER BY [ch_px]";
            DropDownList_chehao.Items.Clear();
            DropDownList_chehao.Items.Add(new ListItem("请选择"));
        }
        else if (DropDownList_lei.SelectedValue == "发电机")
        {
            DropDownList_zrbz.Items.Clear();
            DropDownList_zrbz.Items.Add(new ListItem("请选择"));
            SqlDataSource_banzu.SelectCommand = "SELECT [ch_pwname] FROM [siteman] WHERE ch_pwname in(SELECT DISTINCT banzu1 FROM tcjcb_fadianji WHERE chejian1='" + DropDownList_sscj.SelectedValue + "') ORDER BY [ch_px]";
            DropDownList_chehao.Items.Clear();
            DropDownList_chehao.Items.Add(new ListItem("请选择"));
        }
    }
    protected void DropDownList_zrbz_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList_lei.SelectedValue == "请选择")
        {
            DropDownList_chehao.Items.Clear();
            DropDownList_chehao.Items.Add(new ListItem("请选择"));
            SqlDataSource_chehao.SelectCommand = "";
        }
        else if (DropDownList_lei.SelectedValue == "汽车")
        {
            DropDownList_chehao.Items.Clear();
            DropDownList_chehao.Items.Add(new ListItem("请选择"));
            SqlDataSource_chehao.SelectCommand = "SELECT chehao FROM tcjcb_qiche WHERE banzu1='" + DropDownList_zrbz.SelectedValue + "' ORDER BY [xuhao]";
        }
        else if (DropDownList_lei.SelectedValue == "轨道车")
        {
            DropDownList_chehao.Items.Clear();
            DropDownList_chehao.Items.Add(new ListItem("请选择"));
            SqlDataSource_chehao.SelectCommand = "SELECT chehao FROM tcjcb WHERE banzu1='" + DropDownList_zrbz.SelectedValue + "' ORDER BY [xuhao]";
        }
        else if (DropDownList_lei.SelectedValue == "发电机")
        {
            DropDownList_chehao.Items.Clear();
            DropDownList_chehao.Items.Add(new ListItem("请选择"));
            SqlDataSource_chehao.SelectCommand = "SELECT chehao FROM tcjcb_fadianji WHERE banzu1='" + DropDownList_zrbz.SelectedValue + "' ORDER BY [xuhao]";
        }
    }
    public void g_bind()
    {
        Label_tiaojian.Text = "当前选择条件:";
        string sqlstr = "select * FROM ranyouxiaohao WHERE 1=1" ;
        if (DropDownList_lei.SelectedValue != "请选择")
        {
            sqlstr += " AND (lei = '" + DropDownList_lei.SelectedValue + "')";
            Label_tiaojian.Text += "类别：" + DropDownList_lei.SelectedValue + "；  ";
        }
        if (DropDownList_mingcheng.SelectedValue != "请选择")
        {
            sqlstr += " AND (mingcheng = '" + DropDownList_mingcheng.SelectedValue + "')";
            Label_tiaojian.Text += "燃油种类：" + DropDownList_mingcheng.SelectedValue + "；  ";
        } if (nian.SelectedValue != "请选择")
        {
            sqlstr += " AND (nian = '" + nian.SelectedValue + "')";
            Label_tiaojian.Text += nian.SelectedValue + "年；  ";
        }
        if (yue.SelectedValue != "请选择")
        {
            sqlstr += " AND (yue = '" + yue.SelectedValue + "')";
            Label_tiaojian.Text += yue.SelectedValue + "月；  ";
        }
        if (DropDownList_sscj.SelectedValue != "请选择")
        {
            sqlstr += " AND (chejian = '" + DropDownList_sscj.SelectedValue + "')";
            Label_tiaojian.Text += "车间：" + DropDownList_sscj.SelectedValue + "；  ";
        }
        if (DropDownList_zrbz.SelectedValue != "请选择")
        {
            sqlstr += " AND (banzu = '" + DropDownList_zrbz.SelectedValue + "')";
            Label_tiaojian.Text += "班组：" + DropDownList_zrbz.SelectedValue + "；  ";
        }
        if (DropDownList_chehao.SelectedValue != "请选择")
        {
            sqlstr += " AND (chehao = '" + DropDownList_chehao.SelectedValue + "')";
            Label_tiaojian.Text += "车号：" + DropDownList_chehao.SelectedValue + "；  ";
        }
        if (DropDownList_shenpi.SelectedValue == "车间未审批")
        {
            sqlstr += " AND (zt =1)";
            Label_tiaojian.Text += "审批情况：" + DropDownList_shenpi.SelectedValue + "；  ";
        }
        else if (DropDownList_shenpi.SelectedValue == "段未审批")
        {
            sqlstr += " AND (zt =2)";
            Label_tiaojian.Text += "审批情况：" + DropDownList_shenpi.SelectedValue + "；  ";
        }
        else if (DropDownList_shenpi.SelectedValue == "段已审批未受理")
        {
            sqlstr += " AND (zt =4)";
            Label_tiaojian.Text += "审批情况：" + DropDownList_shenpi.SelectedValue + "；  ";
        }
        else if (DropDownList_shenpi.SelectedValue == "材料科已受理")
        {
            sqlstr += " AND (zt =5)";
            Label_tiaojian.Text += "审批情况：" + DropDownList_shenpi.SelectedValue + "；  ";
        }
        else if (DropDownList_shenpi.SelectedValue == "全部")
        {
            sqlstr += " AND (zt>0)";
            Label_tiaojian.Text += "审批情况：" + DropDownList_shenpi.SelectedValue + "；  ";
        }
        else if (DropDownList_shenpi.SelectedValue == "全部段已审批")
        {
            sqlstr += " AND (zt>=4)";
            Label_tiaojian.Text += "审批情况：" + DropDownList_shenpi.SelectedValue + "；  ";
        }
        if (DropDownList_liezhi.SelectedValue == "全部")
        {
            Label_tiaojian.Text += "列支情况：" + DropDownList_liezhi.SelectedValue + "；  ";
        }
        else if (DropDownList_liezhi.SelectedValue == "成本")
        {
            sqlstr += " AND (liezhi ='成本')";
            Label_tiaojian.Text += "列支情况：" + DropDownList_liezhi.SelectedValue + "；  ";
        }
        else if (DropDownList_liezhi.SelectedValue == "静态检测")
        {
            sqlstr += " AND (liezhi ='静态检测')";
            Label_tiaojian.Text += "列支情况：" + DropDownList_liezhi.SelectedValue + "；  ";
        }
        else if (DropDownList_liezhi.SelectedValue == "达速施工")
        {
            sqlstr += " AND (liezhi ='达速施工')";
            Label_tiaojian.Text += "列支情况：" + DropDownList_liezhi.SelectedValue + "；  ";
        }
        else if (DropDownList_liezhi.SelectedValue == "其他")
        {
            sqlstr += " AND (liezhi ='其他')";
            Label_tiaojian.Text += "列支情况：" + DropDownList_liezhi.SelectedValue + "；  ";
        }
        sqlstr += " ORDER BY nian desc,convert(int,yue) desc,addtime desc ";
        if (Label_tiaojian.Text == "当前选择条件:")
        {
            Label_tiaojian.Text = "当前选择条件:无";
        }
        //Response.Write(sqlstr);
        //Response.End();
        //sqlcon = new SqlConnection(strCon);
        //SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
        //DataSet myds = new DataSet();
        ////sqlcon.Open();
        //myda.Fill(myds, "ganbutiancheng");
        //GridView1.DataSource = myds;
        SqlDataSource1.ConnectionString = strCon;
        SqlDataSource1.SelectCommand = sqlstr;
        GridView1.DataSourceID = SqlDataSource1.ID;
        //GridView1.EnableSortingAndPagingCallbacks = false;
        GridView1.PagerStyle.Font.Size = 10;
        //GridView1.PagerSettings.Position = PagerPosition.TopAndBottom;
        //GridView1.PagerStyle.Font.Bold = true;
        //GridView1.DataBind();
        if (DropDownList_shenpi.SelectedValue == "材料科已受理" || DropDownList_shenpi.SelectedValue == "段已审批未受理" || DropDownList_shenpi.SelectedValue == "全部段已审批")
        {
            try
            {
                //Response.Write(sqlstr);
                //Response.Write(sqlstr.Replace("*", "id"));
                //Response.End();
                string sqlstr1 = "SELECT SUM(jine) AS zongjia,round(SUM(duanshuliang),0) AS zongshu FROM  ranyouxiaohao WHERE     (id IN (" + sqlstr.Replace("*", "id").Replace(" ORDER BY nian desc,convert(int,yue) desc,addtime desc ", "") + ")) AND (duanshuliang <> 0)";
                //Response.Write(sqlstr1);
                sqlcon = new SqlConnection(strCon);
                SqlDataAdapter myda1 = new SqlDataAdapter(sqlstr1, sqlcon);
                DataSet myds1 = new DataSet();
                //sqlcon.Open();
                myda1.Fill(myds1, "siteman1");
                if (myds1.Tables[0].Rows.Count != 0)
                {
                    label_zongjia.Visible = true;
                    label_zongjia.Text = "当前选择条件下计划单的总金额为：" + myds1.Tables[0].Rows[0]["zongjia"].ToString().Replace(".0000", "") + "元，总数量为：" + myds1.Tables[0].Rows[0]["zongshu"].ToString()+"升";
                    if (label_zongjia.Text == "当前选择条件下计划单的总金额为：元，总数量为：升")
                    {
                        label_zongjia.Visible = false;
                    }
                }
            }
            catch (SqlException)
            {
                throw;
            }
        }
        else
        {
            label_zongjia.Visible = false;
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        g_bind();
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        DropDownList_lei.SelectedValue = "请选择";
        DropDownList_mingcheng.SelectedValue = "请选择";
        nian.SelectedValue = "请选择";
        yue.SelectedValue = "请选择";
        DropDownList_sscj.Items.Clear();
        DropDownList_sscj.Items.Add(new ListItem("请选择"));
        DropDownList_zrbz.Items.Clear();
        DropDownList_zrbz.Items.Add(new ListItem("请选择"));
        DropDownList_chehao.Items.Clear();
        DropDownList_chehao.Items.Add(new ListItem("请选择"));
        DropDownList_shenpi.SelectedValue = "全部";
        DropDownList_liezhi.SelectedValue = "全部";
        nian.SelectedValue = DateTime.Now.Year.ToString();
        //yue.SelectedValue = DateTime.Now.Month.ToString();
        SqlDataSource_chejian.SelectCommand = chejiansql;
        g_bind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        g_bind();
        ImageButton1.Focus();
   }
    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        GridViewRow btrow = GridView1.BottomPagerRow;
        Label lb1 = new Label();
        lb1.Text = "当前页码(" + (GridView1.PageIndex + 1) + "/" + GridView1.PageCount + ")";
        if (btrow != null)
        {
            btrow.Cells[0].Controls.Add(lb1);        
        }

    }
    protected void GridView1_PageIndexChanged(object sender, EventArgs e)
    {

    }

}
